Part Number Hot Search : 
FR256 CM200 FR256 MS470 1E106 LT1931A 030000 LT1016
Product Description
Full Text Search
 

To Download AN1780 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  AN1780/1204 1/9 AN1780 application note str71x real time cloc k application example introduction the purpose of this application note is to expl ain how to use the str71x real time clock (rtc) peripheral. as an application example, it demonstrates how to setup the rtc periph - eral, in terms of prescaler and interrupts, to be used to keep the time and date and to generate alarm interrupts . the str71x rtc has a 32-bit binary counter register. the 32-bit binary counters are designed to continuously count time in seconds. this application note addresses how to convert the 32-bit time value into a date and time value that can be put in the following - form mm/dd/yyyy, hh:mm:ss. the date used in this application note is the same used with the unix operating system, the reference date january 1, 1970, often referred to as unix epoch. this document in organized in two sections, the first presents the str71x rtc peripheral and how to configure it and generate a fixed time base, the second part describes the related soft - ware required for the application. this application note deals with various te chniques for keeping time with the str71x. for more information about the str71x rtc peripheral refer to str71x reference manual. note : because of the reference date used and the use of a 32-bit counter, this algorithm rolls over on tuesday, january 19, 03:14:07, 2038. the following application note developed us ing rvdk toolchain v1.6.1 and the str710 eval - board. you must have the arm real view developer kit to get the most out of this application note. 1
2/9 str71x real time clock application example 1 what is a real time clock? a real time clock is a clock that keeps track of the time even when the system is turned off. in contrast, clocks that are not real-time do not function when the system is off. most of the real- time clocks operate at 32.768khz. 2 applications?time and attendance real time clocks are usually found in portabl e systems such as a data collection terminals and smart card readers which are required to keep track of the day and time of certain tasks taking place. after the tasks are completed, usually the portable system will return to a stand- by mode to conserve power. the alarm can be set to wake up the system at certain time inter - vals to perform other tasks or to repeat the proc ess. for example, in an access control appli - cation, when someone tries to access the building through certain doors, the day and time of the entry is recorded and this information can be used for accounting and security purposes, etc. 3 str71x rtc peripheral the rtc peripheral implemented in the str71x family is a 32-bit continuously running counter that can be used, with a few configurati on parameters, to provide a precise clock-cal - endar function. the rtc peripheral is mapped on the apb2 brid ge and clocked by an external 32.768 khz os - cillator through a 32-bit prescaler register, this prescaler can be used to vary the rtc clock from 32.768 khz to 0.5 hz. figure 1. rtc peripheral in this application, the rtc counter registers have to be incremented every second, this can be configured by using a prescaler value of 0x8000 (32768) to slow down the rtc clock from 32768 hz to 1 hz. this allows the rtc to keep time in seconds in the 32-bit counte r. 20-bit prescaler register rtc (32-bit) programmable counter registers 32.768 khz 2
3/9 str71x real time clock application example 4 configuration mode to write in the rtc_prl, rtc_cnt, rtc_alr registers, the peripheral must enter config - uration mode. this is done by setting the cnf bit in the rtc_crl register. in addition, writing to any rtc register is onl y enabled if the previous write operation is fin - ished. to enable the software to detect this situation, the rtoff status bit is provided in the rtc_cr register to indicate that an update of the registers is in progress. a new value can be written to the rtc counters only when the rtoff status bit value is ?1?. configuration procedure : 1. poll rtoff, wait until its value goes to ?1? 2. set cnf bit to enter configuration mode 2. write to one or more rtc registers 3 clear cnf bit to exit configuration mode the write operation only executes when the cn f bit is cleared and it takes at least two clock32 cycles to complete. 5 32-bit counter time conversion this application note explains how to convert the 32-bit time value into a date and time value that can be put in the form of mm/dd/yyyy, hh:mm:ss. many functions provided by the arm compiler used to convert from a date and time to seconds are also described. these functions are modified in order to explore the str71x rtc to deliver time and date. 5.1 c library functions used 5.1.1 rtc prescaler initialisation: before start-up we must adjust the str71x rtc prescaler register value in order to have the rtc counter registers incremented every second, this can be configured by using a prescaler value of 0x8000 (32768) to slow down the rtc clock from 32768 hz to 1 hz. 5.1.2 origin date adjust since the date used in this application note is the same used with the unix operating system, the reference date january 1, 1970, often referred to as unix epoch, we must a dd this number 0x3ff36300 to the value obtained from the str71x rtc counter register with intent to have a start date = 01/01/2004.
4/9 str71x real time clock application example note : 0x3ff36300 is the number of seconds from 01/01/1970 to 01/01/2004. 5.1.3 c functions used 5.1.3.1 localtime converts time in seconds since unix epoch to local time. synopsis #include extern struct tm *localtime(const time_t * /*timer*/); description converts the calendar time pointed to by timer into a broken-down time, expressed a local time. returns returns pointer (ptim) to static data, it converts a time as returned by the time function to a 9-element list with the time analysed for the local time zone. typically use the following tm structure: struct tm { int tm_sec; /* seconds after the minute, 0 to 60 (0 - 60 allows for the occasional leap second) */ int tm_min; /* minutes after the hour, 0 to 59 */ int tm_hour; /* hours since midnight, 0 to 23 */ int tm_mday; /* day of the month, 1 to 31 */ int tm_mon; /* months since january, 0 to 11 */ int tm_year; /* years since 1900 */ int tm_wday; /* days since sunday, 0 to 6 */ int tm_yday; /* days since january 1, 0 to 365 */ int tm_isdst; /* daylight savings time flag */ }; 5.1.3.2 mktime() converts local time to seconds since the unix epoch synopsis #include extern time_t mktime(struct tm * /*timeptr*/); description the mktime() function converts the broken-down time, expressed as local time, in the struc - ture pointed to by timeptr into a calendar ti me value with the same encoding as that of the values returned by the time() function.
5/9 str71x real time clock application example 5.1.4 code description this application note addresses how to convert the 32-bit time value into a date and time value that can be put in the form of mm/dd/yyyy, hh:mm:ss. the code should be set up so that: ? the uart0 and hyperterminal are used to enter a current time and date after start-up. ? the following hyperterminal configuration is used: hyperterminal configuration : - bits per second ---- > 38 400 - data bit --------------- > 8 - parity ----------------- > none - stop bits ------------- > 1 - flow control --------- > none ? after reset, the rtc prescaler is configured to have the rtc counter incremented every sec - ond. ? alarm configuration (an alarm is configured after 5s). ? then enable the rtc alarm interrupt via the eic configuration. ? a current date and time will display on the lcd. the rtc interrupt service routine: ? clear the rtc interrupt flag. ? the rtc alarm will be fired after 5 second and the rtc_irqhandler routine will be execut - ed. an alarm message will be displayed for 2s and the rtc will be configured to generate an other alarm interrupt after 5s. figure 2 shows the code flow of the algorithm used to convert raw seconds to a date/time.
6/9 str71x real time clock application example figure 2. c code flow: here is a section of code taken from main.c which performs the steps shown above in figure 2 above: #include "71x_lib.h" #include "lcd.h" #include #include #include #define rtc_presc 0x8000 adjust current date and time using hyperterminal and uart0 use localtime function in order to convert the current date and time to seconds since unix epoch substract this number 0x3ff36300 from the result in order to have reference date first january 2004 update the rtc device counter use a prescaler value of 0x8000 in order to have the rtc counter registers incremented every one second add this number 0x3ff36300 to the counter register value in order to have a start date = 01/01/70 use mktime function to convert the calendar time pointed to by timer into a broken-down time read the rtc counter register value main display time and date configure the rtc alarm rtc irqhandler clear air and gir bits configure an alarm after 5 seconds display an alarm message
7/9 str71x real time clock application example time_t time; struct tm *ptim; rccu_resetsources resetsources; int main(void) { rtc_flagclear ( rtc_owir ); rtc_flagclear ( rtc_air ); rtc_flagclear ( rtc_sir ); rtc_flagclear ( rtc_gir ); rtc_prescalerconfig(rtc_presc); ptim=malloc(sizeof(*ptim)); printf("s:"); scanf("%d, ",&ptim->tm_sec); printf("m:"); scanf("%d, ",&ptim->tm_min); printf("h:"); scanf("%d",&ptim->tm_hour); printf("d:"); scanf("%d,",&ptim->tm_mday); printf("m:"); scanf("%d,",&ptim->tm_mon); ptim->tm_mon--; printf("y:"); scanf("%d",&ptim->tm_year); ptim->tm_year-=1900; time = mktime(ptim)-0x3ff36300; rtc_waitforlasttask(); rtc_entercfgmode(); // enter in configuration mode rtc_waitforlasttask(); // wait for last task completion rtc->cntl = time & 0x0000ffff; rtc_waitforlasttask(); // wait for last task completion rtc->cnth = ( time & 0xffff0000 ) >> 16; rtc_waitforlasttask(); // wait for last task completion rtc_exitcfgmode (); // exit from configuration mode // rtc_alarmconfig(time+15); rtc_alarmconfig(rtc_countervalue()+5); eic_irqchannelconfig( rtc_irqchannel, enable );// enable rtc irq channel eic_irqchannelpriorityconfig( rtc_irqchannel, 1); eic_irqconfig( enable ); rtc_itconfig( rtc_ait|rtc_git, enable ); // enable alarm interrupt // lcd_underlinecursoroff(); gotoxy(1,1); printf(" "); gotoxy(1,2);
8/9 str71x real time clock application example printf(" "); while(1) { time=rtc_countervalue()+0x3ff36300; ptim = localtime(&time); gotoxy(1,1); printf(" %02d/",ptim->tm_mday); printf("%02d/",ptim->tm_mon+1); printf("%4d\r\n",ptim->tm_year+1900); gotoxy(1,2); printf(" %02d : ",ptim->tm_hour); printf("%02d : ",ptim->tm_min); printf("%02d\r\n",ptim->tm_sec); } }
9/9 str71x real time clock application example ?the present note which is for guidance only aims at providing customers with information regarding their products in order for them to save time. as a result, stmicroelectronics shall not be held liable for any direct, indirect or consequential damages with respect to any claims arising from the content of such a note and/or the use made by customers of the information contained herein in connection with their products.? information furnished is believed to be accurate and reliable. however, stmicroelectronics assumes no responsibility for the co nsequences of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. no license is granted by implication or otherwise under any patent or patent rights of stmicroelectronics. specifications mentioned in this publicati on are subject to change without notice. this publication supersedes and replaces all information previously supplied. stmicroelectronics prod ucts are not authorized for use as critical components in life support devices or systems without express written approval of stmicroelectro nics. the st logo is a registered trademark of stmicroelectronics. all other names are the property of their respective owners ? 2004 stmicroelectronics - all rights reserved stmicroelectronics group of companies australia ? belgium - brazil - canada - china ? czech republic - finland - france - germany - hong kong - india - israel - ital y - japan - malaysia - malta - morocco - singapore - spain - sweden - switzerland - united kingdom - united states of america www.st.com


▲Up To Search▲   

 
Price & Availability of AN1780

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X